﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using Workflow2211A.Application.DTO.Inputs.IndependentBilling;
using Workflow2211A.Application.DTO.Outputs.IndependentBilling;
using Workflow2211A.Application.Services;
using Workflow2211A.Application.Services.IndependentBilling;
using Workflow2211A.Domain.IndependentBilling.CheckupItem;
using Workflow2211A.Domain.IndependentBilling.DrugCategory;
using Workflow2211A.Domain.IndependentBilling.MedicalInstitution;
using Workflow2211A.Domain.IndependentBilling.TestItem;
using System.Linq; // Added for .Sum()

namespace Workflow2211A.API.Controllers.IndependentBilling
{
    /// <summary>
    /// 购物车请求DTO
    /// </summary>
    public class AddToCartDTO
    {
        /// <summary>
        /// 项目类型（1-药品，2-检查，3-检验）
        /// </summary>
        public int ItemType { get; set; }
        
        /// <summary>
        /// 项目ID
        /// </summary>
        public long ItemId { get; set; }
        
        /// <summary>
        /// 数量
        /// </summary>
        public int Quantity { get; set; }
        
        /// <summary>
        /// 会话ID
        /// </summary>
        public string SessionId { get; set; }
    }

    /// <summary>
    /// 创建申请单DTO
    /// </summary>
    public class CreatePrescriptionDTO
    {
        /// <summary>
        /// 会话ID
        /// </summary>
        public string SessionId { get; set; }
        
        /// <summary>
        /// 院区ID
        /// </summary>
        public long HospitalAreaId { get; set; }
        
        /// <summary>
        /// 用户
        /// </summary>
        public string Names { get; set; }
        
        /// <summary>
        /// 开单原因
        /// </summary>
        public string Reason { get; set; }
    }

    /// <summary>
    /// 开单管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IndependentBillingController : ControllerBase
    {
        /// <summary>
        /// 开单服务
        /// </summary>
        private readonly IBillingServices _billingServices;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="billingServices">开单服务</param>
        public IndependentBillingController(IBillingServices billingServices)
        {
            _billingServices = billingServices;
        }
        /// <summary>
        /// 显示所有医疗机构
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<MedicalInstitutionModel> ShowMedicalInstitution()
        {
            return _billingServices.ShowMedicalInstitution();
        }

        /// <summary>
        /// 显示所有检查项目分类
        /// </summary>
        /// <returns>检查项目分类列表</returns>
        [HttpGet]
        public List<CheckupCategoryModel> ShowCheckupCategories()
        {
            return _billingServices.ShowCheckupCategories();
        }

        /// <summary>
        /// 显示所有药品分类
        /// </summary>
        /// <returns>药品分类列表</returns>
        [HttpGet]
        public List<DrugCategoryModel> ShowDrugCategories()
        {
            return _billingServices.ShowDrugCategories();
        }

        /// <summary>
        /// 显示所有检验项目分类
        /// </summary>
        /// <returns>检验项目分类列表</returns>
        [HttpGet]
        public List<TestCategoryModel> ShowTestCategories()
        {
            return _billingServices.ShowTestCategories();
        }

        /// <summary>
        /// 获取药品信息
        /// </summary>
        /// <param name="categoryName">药品分类名称</param>
        /// <returns>药品信息列表</returns>
        [HttpGet]
        public List<DrugInfoOutput> GetDrugsByCategoryName(string categoryName)
        {
            return _billingServices.GetDrugsByCategoryName(categoryName);
        }

        /// <summary>
        /// 获取检查项目信息
        /// </summary>
        /// <param name="checkupCategoryName">检查项目分类名称</param>
        /// <returns>检查项目信息列表</returns>
        [HttpGet]
        public List<CheckupItemOutput> GetCheckupItemsByCategoryName(string checkupCategoryName)
        {
            return _billingServices.GetCheckupItemsByCategoryName(checkupCategoryName);
        }

        /// <summary>
        /// 获取检验项目信息
        /// </summary>
        /// <param name="testCategoryName">检验项目分类名称</param>
        /// <returns>检验项目信息列表</returns>
        [HttpGet]
        public List<TestItemOutput> GetTestItemsByCategoryName(string testCategoryName)
        {
            return _billingServices.GetTestItemsByCategoryName(testCategoryName);
        }
        
       
        
        /// <summary>
        /// 添加开单申请
        /// </summary>
        /// <param name="prescriptionDTO">开单申请信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public IActionResult AddPrescriptionApplication([FromBody] PrescriptionApplicationDTO prescriptionDTO)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                
                long prescriptionId = _billingServices.AddPrescriptionApplication(prescriptionDTO);
                return Ok(new { PrescriptionId = prescriptionId, Message = "开单申请提交成功" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { Message = ex.Message });
            }
        }

        /// <summary>
        /// 确认开单申请
        /// </summary>
        /// <param name="prescriptionId">开单申请ID</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public IActionResult ConfirmPrescriptionApplication(long prescriptionId)
        {
            try
            {
                bool result = _billingServices.ConfirmPrescriptionApplication(prescriptionId);
                return Ok(new { Success = result, Message = "开单申请确认成功" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { Success = false, Message = ex.Message });
            }
        }

       
    }
}
